package com.dy.分类.二分查找和二叉排序树.序列化和反序列化二叉搜索树449;

import com.dy.树.TreeNode;

/*
序列化是将数据结构或对象转换为一系列位的过程，以便它可以存储在文件或内存缓冲区中，或通过网络连接链路传输，以便稍后在同一个或另一个计算机环境中重建。

设计一个算法来序列化和反序列化二叉搜索树。 对序列化/反序列化算法的工作方式没有限制。 您只需确保二叉搜索树可以序列化为字符串，并且可以将该字符串反序列化为最初的二叉搜索树。

编码的字符串应尽可能紧凑。

注意：不要使用类成员/全局/静态变量来存储状态。 你的序列化和反序列化算法应该是无状态的。
 */
public class Solution {

}

class Codec1 {

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuilder sb = new StringBuilder("");
        encoderHelper(root, sb);
        return sb.toString();
    }

    private void encoderHelper(TreeNode root, StringBuilder sb) {
        if (root == null) {
            sb.append("#,");
            return;
        }
        sb.append(root.val);
        sb.append(",");
        encoderHelper(root.left, sb);
        encoderHelper(root.right, sb);
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        String s[] = data.split(",");
        int index[] = new int[1];
        TreeNode res = decoderHelper(s, index);
        return res;
    }

    private TreeNode decoderHelper(String[] s, int[] index) {

        if (s[index[0]].equals("#")) {
            index[0]++;
            return null;
        }
        TreeNode treeNode = new TreeNode(Integer.valueOf(s[index[0]++]));
        treeNode.left = decoderHelper(s, index);
        treeNode.right = decoderHelper(s, index);
        return treeNode;

    }
}

class Codec2 {

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuilder sb = new StringBuilder("");
        encoderHelper(root, sb);
        return sb.toString();
    }

    private void encoderHelper(TreeNode root, StringBuilder sb) {
        if (root == null) {
            return;
        }
        sb.append(root.val);
        sb.append(",");
        encoderHelper(root.left, sb);
        encoderHelper(root.right, sb);
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        if (data.length() == 0) return null;
        String s[] = data.split(",");

        TreeNode res = decoderHelper(s, 0, s.length - 1);
        return res;
    }

    private TreeNode decoderHelper(String[] s, int start, int end) {

        if (start>end) {
            return null;
        }
        TreeNode treeNode = new TreeNode(Integer.parseInt(s[start]));
        int i;
        for (i = start + 1; i <= end; i++) {
            if (Integer.parseInt(s[i]) > Integer.parseInt(s[start])) {
                break;
            }
        }
        treeNode.left = decoderHelper(s, start + 1, i - 1);
        treeNode.right = decoderHelper(s, i, end);
        return treeNode;

    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(2);
        root.right = new TreeNode(3);
        Codec2 codec2 = new Codec2();
        String s =codec2.serialize(root);
        System.out.println(s);
        codec2.deserialize(s);
        System.out.println();
    }
}
